package com.xrz.sxm.aj.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xrz.sxm.aj.activity.Act_XueWei;
import com.xrz.sxm.aj.entity.XueweiEntity;

import java.util.List;
import java.util.Map;

public class TableView extends LinearLayout {

	private Map<Integer, List<XueweiEntity>> m_Xueweis;

	private LinearLayout m_LinearLayout;

	public TableView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public TableView(Context context) {
		super(context);
		init();
	}

	public void setXueweis(Map<Integer, List<XueweiEntity>> maps) {
		this.m_Xueweis = maps;
		for (Map.Entry<Integer, List<XueweiEntity>> entry : m_Xueweis
				.entrySet()) {
			createOneDayLine(entry.getKey(), entry.getValue());
		}
	}

	private void init() {
		setOrientation(VERTICAL);
		m_LinearLayout = new LinearLayout(getContext());
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		m_LinearLayout.setLayoutParams(layoutParams);
		m_LinearLayout.setOrientation(VERTICAL);
		m_LinearLayout.setBackgroundColor(Color.BLACK);
		addView(m_LinearLayout);
		createTitle();
	}

	/** 生成表头 */
	private void createTitle() {
		LinearLayout layout = new LinearLayout(getContext());
		layout.setOrientation(HORIZONTAL);
		// 表头：灸序
		TextView tv_jiuxu = createNomalTextView("灸序", 2, 2, 0, 2, 0.8f);
		layout.addView(tv_jiuxu);

		LinearLayout layout2 = new LinearLayout(getContext());
		// 表头：穴位名
		TextView tv_xueweiming = createNomalTextView("穴位名", 2, 2, 0, 2, 1);
		layout2.addView(tv_xueweiming);
		// 表头：
		TextView tv_wendu = createNomalTextView("参考温度", 2, 2, 0, 2, 1);
		layout2.addView(tv_wendu);
		// 表头：
		TextView tv_shijian = createNomalTextView("参考时间", 2, 2, 0, 2, 1);
		layout2.addView(tv_shijian);
		// 表头：
		TextView tv_beizhu = createNomalTextView("备注", 2, 2, 2, 2, 0.8f);
		layout2.addView(tv_beizhu);

		LayoutParams params2 = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
		params2.weight = 4;
		layout.addView(layout2, params2);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		m_LinearLayout.addView(layout, params);
	}

	private void createOneDayLine(int day, List<XueweiEntity> list) {
		LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT);
		params.weight = 0.8f;
		params.leftMargin = 2;
		params.bottomMargin = 2;
		LinearLayout layout = new LinearLayout(getContext());
		layout.setOrientation(HORIZONTAL);
		TextView tv_day = createNomalTextView("第" + day + "天", 2, 0, 0, 2, 0.8f);
		layout.addView(tv_day, params);
		LinearLayout layout2 = new LinearLayout(getContext());
		layout2.setOrientation(VERTICAL);
		for (int i = 0; i < list.size(); i++) {
			LinearLayout layout3 = createLine(i, list.get(i),
					i == list.size() - 1);
			layout2.addView(layout3);
		}
		LayoutParams params2 = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
		params2.weight = 4;
		layout.addView(layout2, params2);
		m_LinearLayout.addView(layout);
	}

	/**
	 * 生成不带天数的一行数据
	 *
	 * @param entity
	 * @return
	 */
	private LinearLayout createLine(int id, XueweiEntity entity,
									boolean lastLine) {
		LinearLayout layout = new LinearLayout(getContext());
		layout.setId(id);
		layout.setOrientation(HORIZONTAL);
		// 表头：穴位名
		TextView tv_xueweiming = createXuweiTextView(entity.xwmc, 2, 0, 0, 1, 1,entity);
		layout.addView(tv_xueweiming);
		// 表头：
		TextView tv_wendu = createNomalTextView("52℃", 2, 0, 0, 1,1);
		layout.addView(tv_wendu);
		// 表头：
		TextView tv_shijian = createNomalTextView(entity.ajsj+"分钟", 2, 0, 0, 1,1);
		layout.addView(tv_shijian);
		// 表头：
		TextView tv_beizhu = createNomalTextView(entity.bz, 2, 0, 2, 1, 0.8f);
		layout.addView(tv_beizhu);
		return layout;
	}

	private TextView createNomalTextView(String text, int left, int top,
										 int right, int bottom, float weight) {
		LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
		params.topMargin = top;
		params.bottomMargin = bottom;
		params.leftMargin = left;
		params.rightMargin = right;
		params.gravity = Gravity.CENTER;
		params.weight = weight;
		TextView tv = new TextView(getContext());
		tv.setLayoutParams(params);
		tv.setText(text);
		tv.setBackgroundColor(Color.parseColor("#bdbdbd"));
		tv.setGravity(Gravity.CENTER);
		tv.setTextSize(15);
		tv.setTextColor(Color.BLACK);
		return tv;
	}

	private TextView createXuweiTextView(final String text, int left, int top,
										 int right, int bottom, float weight, final XueweiEntity entity) {
		final TextView tv = createNomalTextView(text, left, top, right, bottom, weight);
		tv.setTextColor(Color.parseColor("#2f4f00"));
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				getContext().startActivity(new Intent(getContext(), Act_XueWei.class).putExtra("data", entity));
			}
		});
		return tv;
	}
}
